package com.yinhe.module.yzx.activity;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

import com.yinhe.common.core.domain.entity.SysUser;
import com.yinhe.common.core.domain.model.LoginUser;
import com.yinhe.common.exception.ServiceException;
import com.yinhe.common.utils.DateUtils;

import com.yinhe.system.domain.YhActivatePaticipant;
import com.yinhe.system.domain.YhContentStatistics;

import com.yinhe.system.msg.mssage.domain.YhMsgInner;
import com.yinhe.system.msg.ws.service.WebSocketService;
import com.yinhe.system.service.IYhActivatePaticipantService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.system.domain.YhActivate;
import com.yinhe.system.service.IYhActivateService;
import com.yinhe.common.utils.poi.ExcelUtil;
import com.yinhe.common.core.page.TableDataInfo;


/**
 * 活动信息Controller
 *
 * @author yinhe
 * @date 2022-09-06
 */
@RestController
@RequestMapping("/yzx/activity/activity")
public class YzxActivityController extends BaseController {
    @Autowired
    private IYhActivateService yhActivateService;
    @Autowired
    public WebSocketService webSocketService;
    @Autowired
    private IYhActivatePaticipantService yhActivatePaticipantService;

    /**
     * 查询活动信息列表
     */
    @PreAuthorize("@ss.hasPermi('yzx:activity:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhActivate yhActivate) {
        startPage();
        List<YhActivate> list = yhActivateService.selectYhActivateList(yhActivate);
        return getDataTable(list);
    }

    /**
     * 导出活动信息列表
     */
    @PreAuthorize("@ss.hasPermi('yzx:activity:activity:export')")
    @Log(title = "活动信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhActivate yhActivate) {
        List<YhActivate> list = yhActivateService.selectYhActivateList(yhActivate);
        ExcelUtil<YhActivate> util = new ExcelUtil<YhActivate>(YhActivate.class);
        util.exportExcel(response, list, "活动信息数据");
    }

    /**
     * 获取活动信息详细信息
     */
    /*@PreAuthorize("@ss.hasPermi('yzx:activity:activity:query')")*/
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(yhActivateService.selectYhActivateById(id));
    }

    /**
     * 新增活动信息
     */
    @PreAuthorize("@ss.hasPermi('yzx:activity:activity:add')")
    @Log(title = "活动信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhActivate yhActivate) {
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        yhActivate.setDeptId(sysUser.getDeptId());
        yhActivate.setUserId(sysUser.getUserId());
        yhActivate.setNickName(sysUser.getNickName());
        /*YhMsgInner msgInner = new YhMsgInner();
        msgInner.setMsgTitle("新增一条活动");
        msgInner.setMsgContent(yhActivate.getActivateTitile());
        msgInner.setSendUserName(sysUser.getUserName());
        msgInner.setContentType("");
        String[] split = yhActivate.getNote3().split(",");
        List<String> receiveNames = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            receiveNames.add(split[i]);
        }
        webSocketService.PushMsg(msgInner, receiveNames);*/
        return toAjax(yhActivateService.insertYhActivate(yhActivate));
    }

    /**
     * 修改活动信息
     */
    @PreAuthorize("@ss.hasPermi('yzx:activity:activity:edit')")
    @Log(title = "活动信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhActivate yhActivate) {
        return toAjax(yhActivateService.updateYhActivate(yhActivate));
    }

    @PreAuthorize("@ss.hasPermi('yzx:activity:activity:bm')")
    @Log(title = "活动信息", businessType = BusinessType.UPDATE)
    @PutMapping("/bm")
    public AjaxResult bm(@RequestBody YhActivate yhActivate) {
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        long r = 0;
        Date d1 = yhActivate.getApplyEndTime();
        String d2 = new String();
        String d3 = new String();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        d2 = DateUtils.dateTimeNow();
        d3 = df.format(d1);
        long i1 = Long.parseLong(d3.trim());
        long i2 = Long.parseLong(d2.trim());
        r = i1 - i2;
        if (r > 0) {
            YhActivatePaticipant p = new YhActivatePaticipant();
            p.setDeptId(sysUser.getDeptId());
            p.setUserId(sysUser.getUserId());
            p.setNickName(sysUser.getNickName());
            p.setActivateId(yhActivate.getId());
            p.setActivateTitile(yhActivate.getActivateTitile());
            p.setActivateIllustrate(yhActivate.getActivateIllustrate());
            p.setActivatePhoto(yhActivate.getActivatePhoto());
            p.setPaticipantState("已报名");
            YhActivatePaticipant t = new YhActivatePaticipant();
            t.setActivateId(yhActivate.getId());
            t.setUserId(sysUser.getUserId());
            List<YhActivatePaticipant> list = yhActivatePaticipantService.selectYhActivatePaticipantList(t);
            if (list.size() == 0) {

                return toAjax(yhActivatePaticipantService.insertYhActivatePaticipant(p));
            } else {
                throw new ServiceException("已报名，请勿重新报名！");
                /*return AjaxResult.success(yhActivateService.selectYhActivateById(yhActivate.getId()));*/
            }
        } else {
            throw new ServiceException("报名时间已过！");
            /*return AjaxResult.success(yhActivateService.selectYhActivateById(yhActivate.getId()));*/
        }
    }

    /**
     * 删除活动信息
     */
    @PreAuthorize("@ss.hasPermi('yzx:activity:activity:remove')")
    @Log(title = "活动信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        YhActivatePaticipant AP = new YhActivatePaticipant();
        for (int i = 0; i < ids.length; i++) {
            AP.setActivateId(ids[i]);
            List<YhActivatePaticipant> LAP = yhActivatePaticipantService.selectYhActivatePaticipantList(AP);
            Long[] lo = new Long[9999];
            for (int k = 0; k < LAP.size(); k++) {

                lo[k] = (long) LAP.get(k).getId();
            }
            yhActivatePaticipantService.deleteYhActivatePaticipantByIds(lo);
        }
        return toAjax(yhActivateService.deleteYhActivateByIds(ids));
    }
}
